﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace ReceiveGroupData
{

    public delegate void RevDataEvevtHandler(Object sender, RevEventArgs e);
    public class RevEventArgs : EventArgs
    {
        public int iProcessCode = 0;//处理主机编号
        public int iSensorCode = 0;//传感器编号
        public bool bSensorCode = false;//传感器数据
        public string strGDDLID = string.Empty;//轨道电路ID
        public bool bGDDLCode = false;//轨道电路数据
    }
    public class GetData
    {
        private UdpClient _udp = null;
        private IPEndPoint _ipe = null;

        public GetData(int iRecPort)
        {
            try
            {
                _udp = new UdpClient(iRecPort);
                _ipe = new IPEndPoint(IPAddress.Any, iRecPort);
            }
            catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.StackTrace + ":" + ex.Message); }
        }
        public void ReceiveData()
        {
            byte[] btmp = null;
            while (true)
            {
                try
                {
                    byte[] bytes = _udp.Receive(ref _ipe);
                    if (!bytes.Equals(btmp))
                    {
                        CGlobal.AddRecData(bytes);
                        btmp = bytes;
                    }
                }
                catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.StackTrace + ":" + ex.Message); }
            }
        }
    }
}
